﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Database.dsRankingTableAdapters;

namespace QLTV.Forms.KhaiBao
{
    public partial class frmEditRank : BaseForm
    {
        public frmEditRank()
        {
            InitializeComponent();
            isInsert = true;
        }
        private bool isInsert = false;
        private BindingSource bindingsource;

        public frmEditRank(BindingSource bindingsource)
        {
            InitializeComponent();
            isInsert = false;
            this.bindingsource = bindingsource;
        }
        private void frmEditRank_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsCertificate.tblCertification' table. You can move, or remove it, as needed.
            this.tblCertificationTableAdapter.Fill(this.dsCertificate.tblCertification);
            // TODO: This line of code loads data into the 'dsRanking.tblCrewType' table. You can move, or remove it, as needed.
            this.tblCrewTypeTableAdapter.Fill(this.dsRanking.tblCrewType);
            // TODO: This line of code loads data into the 'dsRanking.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsRanking.tblRank);
            if (isInsert)
            {
                this.btnAdd.Enabled = true;
                this.btnSave.Enabled = false;
                this.tblRankBindingSource.AddNew();
            }
            else
            {
                this.btnAdd.Enabled = false;
                this.btnSave.Enabled = true;
                this.tblRankBindingSource.Position = this.bindingsource.Position;

                QLTV.Database.dsRanking.tblRankRow rowRank = (QLTV.Database.dsRanking.tblRankRow)((System.Data.DataRowView)tblRankBindingSource.Current).Row;

                using (QLTV.Database.dsCertificateTableAdapters.tblCertificationRequireTableAdapter certificateRequireAdapter = new Database.dsCertificateTableAdapters.tblCertificationRequireTableAdapter())
                {
                    QLTV.Database.dsCertificate.tblCertificationRequireDataTable tblCertificateRequire = certificateRequireAdapter.GetDataBy_RankId(rowRank.nRankId);
                    if (tblCertificateRequire.Rows.Count > 0)
                        for (int i = 0; i < tblCertificateRequire.Rows.Count; i++)
                        {
                            QLTV.Database.dsCertificate.tblCertificationRequireRow rowCertificate = tblCertificateRequire[i];
                            for (int j = 0; j < cklCertificate.ItemCount; j++)
                                if (rowCertificate.nCertificationId == ((QLTV.Database.dsCertificate.tblCertificationRow)((System.Data.DataRowView)cklCertificate.GetItem(j)).Row).nCertificationId)
                                    cklCertificate.SetItemChecked(j, true);
                        }
                }
            }
        }

        public delegate void AddHandler(object sender, EventArgs e);
        public event AddHandler AddClick;

        void onAdd(object sender, EventArgs e)
        {
            if (AddClick != null)
            {
                AddClick(sender, e);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.tblRankBindingSource.EndEdit();
            this.tblRankTableAdapter.Update(dsRanking.tblRank);

            QLTV.Database.dsRanking.tblRankDataTable tblRank = tblRankTableAdapter.GetLastedRow();
            QLTV.Database.dsRanking.tblRankRow rowRank = tblRank[0];

            QLTV.Database.dsCertificate.tblCertificationRequireDataTable tblCertificateReqiere = new Database.dsCertificate.tblCertificationRequireDataTable();

            foreach (System.Data.DataRowView item in cklCertificate.CheckedItems)
            {
                QLTV.Database.dsCertificate.tblCertificationRequireRow rowCertificate = tblCertificateReqiere.NewtblCertificationRequireRow();

                rowCertificate.nCertificationId = ((QLTV.Database.dsCertificate.tblCertificationRow)item.Row).nCertificationId;
                rowCertificate.nRankId = rowRank.nRankId;

                tblCertificateReqiere.AddtblCertificationRequireRow(rowCertificate);
            }

            using (QLTV.Database.dsCertificateTableAdapters.tblCertificationRequireTableAdapter certificateRequireAdapter = new Database.dsCertificateTableAdapters.tblCertificationRequireTableAdapter())
            {
                certificateRequireAdapter.Update(tblCertificateReqiere);
            }

            onAdd(sender, e);

            //this.tblRankTableAdapter.Fill(this.dsRanking.tblRank);
            this.tblRankBindingSource.AddNew();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.tblRankBindingSource.EndEdit();
            this.tblRankTableAdapter.Update(dsRanking.tblRank);

            QLTV.Database.dsRanking.tblRankRow rowRank = (QLTV.Database.dsRanking.tblRankRow)((System.Data.DataRowView)tblRankBindingSource.Current).Row;
            using (QLTV.Database.dsCertificateTableAdapters.tblCertificationRequireTableAdapter certificateRequireAdapter = new Database.dsCertificateTableAdapters.tblCertificationRequireTableAdapter())
            {
                QLTV.Database.dsCertificate.tblCertificationRequireDataTable tblCertificateRequire = certificateRequireAdapter.GetDataBy_RankId(rowRank.nRankId);
                if (tblCertificateRequire.Rows.Count > 0)
                {
                    for (int i = 0; i < tblCertificateRequire.Rows.Count; i++)
                    {
                        QLTV.Database.dsCertificate.tblCertificationRequireRow rowCertificate = tblCertificateRequire[i];
                        rowCertificate.Delete();
                    }
                    certificateRequireAdapter.Update(tblCertificateRequire);
                }
                   

                using (QLTV.Database.dsCertificate.tblCertificationRequireDataTable tblNewCertificateReqiere = new Database.dsCertificate.tblCertificationRequireDataTable())
                {
                    foreach (System.Data.DataRowView item in cklCertificate.CheckedItems)
                    {
                        QLTV.Database.dsCertificate.tblCertificationRequireRow rowCertificate = tblNewCertificateReqiere.NewtblCertificationRequireRow();

                        rowCertificate.nCertificationId = ((QLTV.Database.dsCertificate.tblCertificationRow)item.Row).nCertificationId;
                        rowCertificate.nRankId = rowRank.nRankId;

                        tblNewCertificateReqiere.AddtblCertificationRequireRow(rowCertificate);
                    }
                    certificateRequireAdapter.Update(tblNewCertificateReqiere);
                }
               
            }

            this.tblRankTableAdapter.Fill(this.dsRanking.tblRank);
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}